
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body" style="height: auto;">
                    <div class="layui-tab">
                        <ul class="layui-tab-title">
                            <li class="layui-this">链接</li>
                            <li>二维码</li>

                        </ul>
                        <div class="layui-tab-content">
                            <div class="layui-tab-item layui-show">
                                <blockquote class="layui-elem-quote" id="share-url"></blockquote>
                                <button type="button" class="layui-btn" id="btn-copy">复制</button>
                            </div>
                            <div class="layui-tab-item" id="share-qrcode"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>

    layui.use('element', function () {
        //定义函数
        window.Clipboard = (function(window, document, navigator) {
            var textArea,
                copy;

            // 判断是不是ios端
            function isOS() {
                return navigator.userAgent.match(/ipad|iphone/i);
            }
            //创建文本元素
            function createTextArea(text) {
                textArea = document.createElement('textArea');
                textArea.value = text;
                document.body.appendChild(textArea);
            }
            //选择内容
            function selectText() {
                var range,
                    selection;

                if (isOS()) {
                    range = document.createRange();
                    range.selectNodeContents(textArea);
                    selection = window.getSelection();
                    selection.removeAllRanges();
                    selection.addRange(range);
                    textArea.setSelectionRange(0, 999999);
                } else {
                    textArea.select();
                }
            }

            //复制到剪贴板
            function copyToClipboard() {
                try{
                    if(document.execCommand("Copy")){
                        layer.msg("复制成功！");
                    }else{
                        layer.msg("复制失败！请手动复制！");
                    }
                }catch(err){
                    layer.msg("复制错误！请手动复制！")
                }
                document.body.removeChild(textArea);
            }

            copy = function(text) {
                createTextArea(text);
                selectText();
                copyToClipboard();
            };

            return {
                copy: copy
            };
        })(window, document, navigator);
    });

    //使用函数
    $("#btn-copy").on("click",function(){
        var  val = $("#share-url")[0].innerText;
        Clipboard.copy(val);
    });

</script>
